package com.ckf.login_wx.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckf.login_wx.entity.Disburse;
import com.ckf.login_wx.entity.Material;
import com.ckf.login_wx.servic.DisburseService;
import com.ckf.login_wx.util.Idname;
import com.github.pagehelper.PageHelper;

@RestController
public class Disbursecontroller {
	@Autowired
    private DisburseService disburseService;
    @PostMapping("/addordex")
    public Map<String, Object> addordex(String purpose,String time,String remark,String money) throws ParseException{
        Map<String, Object> map = new HashMap<>();
        Disburse disburse=new Disburse();
        disburse.setMoney(Double.parseDouble(money));
        disburse.setPurpose(purpose);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        disburse.setTime(simpleDateFormat.parse(time));
        disburse.setRemark(remark);
		if (disburseService.save(disburse)){
            map.put("code",200);
            map.put("result","添加成功");
            System.out.println("添加成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }
    @GetMapping("/exlist")
    public Object exlist(String pagenew){
        Map<String, Object> map = new HashMap<>();
        List<Disburse> list=disburseService.list();
        map.put("list", list);
        double sum=0.00;
        for (int i = 0; i < list.size(); i++) {
        	sum+=list.get(i).getMoney();
		}
        map.put("sum", sum);
        return map;
    }
    @GetMapping("/exlistall")
    public Object exlistall(){
        return disburseService.list();
    }
    @GetMapping("/deledisbursebyid")
    public void deledisbursebyid(Integer id){
    	disburseService.removeById(id);
    }
    @GetMapping("/disburse/search")
    public Object disbursesearch(String talk,String search){
        Map<String, Object> map = new HashMap<>();
        List<Disburse> list=disburseService.list(new QueryWrapper<Disburse>().like(Idname.getname(search), talk));
        map.put("list", list);
        double sum=0.00;
        for (int i = 0; i < list.size(); i++) {
        	sum+=list.get(i).getMoney();
		}
        map.put("sum", sum);
        return map;
    }
    @GetMapping("/orddisbursebysearch")
    public Object orddisbursebysearch(String search,String talk,String pagenew){//
        Integer i = Integer.parseInt(pagenew);
    	if(search.equals("时间")){
       	 	PageHelper.offsetPage((i-1)*5, 5);
    		return disburseService.seletime(talk);
    	}
//        QueryWrapper<Disburse> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
   	 	PageHelper.offsetPage((i-1)*5, 5);
        return disburseService.selectlike(Idname.getname(search), talk);
    }
    @GetMapping("/orddisbursebysearchall")
    public Object orddisbursebysearchall(String search,String talk){//
    	if(search.equals("时间")){
    		return disburseService.seletime(talk);
    	}
//        QueryWrapper<Disburse> queryWrapper = new QueryWrapper<>();
//        Map<String, Object> mapuser = new HashMap<>();
//        mapuser.put(Idname.getname(search),talk);
//        queryWrapper.allEq(mapuser);
        return disburseService.selectlike(Idname.getname(search), talk);
    }
    @GetMapping("/seledisbursebyid")
    public Object seledisbursebyid(String id){
    	Integer idint = Integer.parseInt(id);
        return disburseService.getById(idint);
    }
    @PostMapping("/updisbursebyid")
    public Object updisbursebyid(String id,String purpose,String time,String remark,String money) throws ParseException{
        Map<String, Object> map = new HashMap<>();
        Integer i = Integer.parseInt(id);
    	Disburse disburse=new Disburse();
    	disburse.setId(i);
        disburse.setMoney(Double.parseDouble(money));
        disburse.setPurpose(purpose);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        disburse.setTime(simpleDateFormat.parse(time));
        disburse.setRemark(remark);
		if (disburseService.updateById(disburse)){
            map.put("code",200);
            map.put("result","修改成功");
            System.out.println("修改成功");
        }else {
        	map.put("code",200);
            map.put("result","no");
        }
        return map;
    }
}
